/**
 * ------------------------------------------------------------------
 *Copyright reserved by Emin Technology
 *------------------------------------------------------------------

 *PROJECT:		eminview-sales
 *FILE:			OrderBy.java
 *AUTHOR:		Senon Ye
 *DATE CREATION:2011-7-16
 *DESCRIPTION:	SortKey

 *==========================================================
 * VERSION CONTROL
 *Number – Date – Author – SVN – Description
 *01 –  2011-7-16 – senon ye – xxx – SortKey
 *-------------------------------------------------------------------------------------------------------
 */

package top.jle.jpa.basic.dao;

import lombok.Getter;
import top.jle.jpa.basic.entities.Entity;

/**
 * 排序键
 * @author jim
 */
@Getter
public class SortKey {
	//默认 ID 倒序
	public static final SortKey DEFAULT = new SortKey(Entity.PROP_ID,false);


	private final String property;

	private final boolean ascendingOrder ;

	/**
	 * 排序键构造函数,若要使用默认id降序,直接使用SortKey.DEFAULT 常量即可,不用调用该构造函数
	 * 
	 * @param property 取值参见实体类(接口)属性,支持关联对象的属性 e.g User中group.id
	 * @param ascendingOrder  a flag that indicates whether to sort in ascending(true) or descending(false) order
	 */
	private SortKey(String property, boolean ascendingOrder ) {
		this.property = property;
		this.ascendingOrder  = ascendingOrder ;
	}

	/**
	 * 升序
	 * 
	 * @param property 取值参见实体类(接口)属性,支持关联对象的属性 e.g User中group.id
	 */
	public static SortKey asc(String property){
		return new SortKey(property,true );
		
	}
	
	/**
	 * 降序
	 * @param property 取值参见实体类(接口)属性,支持关联对象的属性 e.g User中group.id
	 * 
	 */
	public static SortKey desc(String property){
		return new SortKey(property,false);
	}


	@Override
	public boolean equals(Object obj) {
		if(super.equals(obj)){
			String objProperty = ((SortKey)obj).getProperty();
			objProperty = objProperty == null?"":objProperty;
			return objProperty.equals(this.property);
		}
		return super.equals(obj);
	}

	@Override
	public int hashCode() {
		
		return super.hashCode()+this.property.hashCode();
	}
	
	

}
